#ifndef __UI_WIDGETS__
#define __UI_WIDGETS__

#include <QObject>
#include <QStack>
#include <QDate>
#include "common.h"

#define MIN_BUTTON_CLICK_TIME		1000				// 1 second

class QTimer;
class UIController_Worker;

class UIWidgetController : public QObject
{
	Q_OBJECT

public:

    UIWidgetController(UIController_Worker *ui_controller, QObject *parent);
    ~UIWidgetController();

    void start();
    uint32_t trans_type() { return m_trans_type;};

signals:

    void set_screen(uint32_t scrn_id);
    void pop_screen(uint32_t scrn_id = 0xffffffff);
    void enter_screen(uint32_t scrn_id);
	void set_btn_status(uint32_t widget_id, uint32_t pressed);
	void set_text(uint32_t widget_id, QString s);
	void set_icon(uint32_t widget_id, uint32_t icon_idx);
	void set_select(uint32_t widget_id, uint32_t val);
	void set_enable(uint32_t widget_id, bool enable);
	void set_visible(uint32_t widget_id, bool show);
	void set_table_data(uint32_t widget_id, QStringList s_list);

	void update_component_status();

	void enter_init_mode();

	void enter_intelligent_mode();
	void enter_mixed_mode();
	void enter_count_mode();
	void enter_transaction_mode();
	void enter_note_transaction_mode();
	void enter_coin_transaction_mode();

	void countinue_count();

	void user_login(QString user_name, QString password);
	void user_logout();
	void user_modify_password(QString user_name, QString old_password, QString new_password);

	void inquiry_data(QDate start_date, QDate stop_date);
	void inquiry_op();
	void add_op(QString user_name, QString user_password, QString station_id);
	void update_op(QString user_name, QString user_password, QString station_id, uint32_t finger_id);
	void delete_op(uint32_t user_idx);
	void edit_op(uint32_t user_idx);
	void fp_enroll(QString user_name);

	void store_note();
	void transaction_count_info(NoteInfo note_info);
	void print_coin_trans();
	void store_coin();
	void finish_transaction(uint8_t store_cmd = 0, uint8_t store_exception = 0);

	void print_current_transaction();
    void print_transaction(unsigned int rec_idx);
    void print_transactions();

    void machine_info(QString name, QString server_name, QString terminal_id);
    void inquiry_clr_data(QDate start_date, QDate stop_date);
    void print_clr_transaction(unsigned int rec_idx);

    void start_clear_deposit();
    void clear_deposit();

public slots:

	void scrn_switched(uint32_t scrn_id);
	void btn_clicked(uint32_t widget_id);
	void txt_changed(uint32_t widget_id, QString s);
	void sel_changed(uint32_t widget_id, uint32_t val);
	void tab_sel_changed(uint32_t widget_id, uint32_t val);
	void set_prompt(uint32_t msg_type, uint32_t msg_id);
	void set_prompt(QString msg);
	void set_nc_fault_prompt(uint16_t error1, uint16_t error2);

	void prt_state_changed(bool online);
	void notecounter_state_changed(bool online);
	void server_state_changed(bool online);
	void bag_state_changed(bool closed);
	void case_door_state_changed(bool closed);

	void display_note(NoteInfo note_info);
	void admin_logged();
	void user_logged();
	void store_note_complete();
	void store_coin_complete();
	void data_show(QStringList s_list);
	void op_data_show(QStringList s_list);
	void clr_data_show(QStringList s_list);
	void clr_total_show(ClearInfo clr_info);
    void do_edit_op(QString op_name, QString op_password, QString terminal_name, uint32_t op_finger_id);

    void count_starting();
    void count_completing();
    void store_starting();
    void store_completing();

    void set_user_name(QString name);
    void set_machine_info(QString name, QString server_name, QString terminal_id);

	void fp_message(uint32_t proc, uint32_t enroll_step);

private slots:

	void update_time();
	void unlock_scrn();
	void scrn_init(uint32_t scrn_id);
	void scrn_pop(uint32_t to_scrn_id);
	void hide_prompt();

private:

	enum {
		WIDGET_TYPE_TEXT,
		WIDGET_TYPE_SELECT,
		WIDGET_TYPE_TABLE
	};
    struct Field {
		uint32_t widget_id;
		uint32_t widget_type;
		QString widget_value;
	};
    typedef struct Field Field;
	struct Screen {
		uint32_t scrn_id;
		QList<Field> fields;
	};
	typedef struct Screen Screen;

private:

	void scrn_widget_init();
	QString fetch_widget_value(uint32_t scrn_id, uint32_t widget_id);

private:

	UIController_Worker *m_ui_controller;
	QTimer *m_second_timer;
	QTimer *m_prompt_timer;

	uint32_t m_curr_scrn_id;
	bool m_btn_locked;
	QList<Screen> m_screens;

	uint32_t m_deposit_category_count;
	NoteInfo m_note_info;
	uint32_t m_trans_type;

	bool m_user_edit;
	bool m_admin_status;
	QStack<uint32_t> m_screen_stack;

	QString m_user_name;
	QString m_machine_name;
	QString m_http_server_name;
	QString m_terminal_id;
};


#endif
